import URL from '../net/url.js';

let instance = null;
let array = new Array();;
export default class UserActionInformation {
    userID ='null';
    userAction = 'null';
    actionTime = 'null';
    myLatitude = 'null';
    myLongitude = 'null';
    myCourseID = 'null';
    myChapterID = 'null';
    myUnitID = 'null';
    myCoursewareID = 'null';
    myActivityID = 'null';
    userState = 'null';
    
    constructor() {  
        if(!instance){  
              instance = this;  
        }  
        return instance;  
    }
    
    getUserActionObj(userAction) {
        var actionObject = {
            "userID":userAction.userID,
            "userAction":userAction.userAction,
            "actionTime":userAction.actionTime,
            "myLatitude":userAction.myLatitude,
            "myLongtitude":userAction.myLongitude,
            "myChapterID":userAction.myChapterID,
            "myUnitID":userAction.myUnitID,
            "myCourseID":userAction.myCourseID,
            "myActivityID":userAction.myActivityID,
            "myCoursewareID":userAction.myCoursewareID,
            "userState":"Online"
        }
        return actionObject;
    }

    addActionArray(jsonObject) {
        
        array.push(jsonObject);
        
        return array;
    }

    getcurrentTime() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
        month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
        return currentdate;
    }

    uploadActionInfo() {
        
        let keymap = {
            method: 'POST',
            headers: { 
                       'Content-Type':'application/json;charset=UTF-8',
                     },    
            body: JSON.stringify({"id":array})
        };
        
        console.log('arg---', keymap);
        var promise = new Promise((resolve, reject) => {
            fetch(URL.uploadActionInfo, keymap).then((result) => {
                
                result.text().then(
                    (resultData) => {
                    }
                ).catch(
                    (error) => {
                        reject(error);
                    }
                    )
            }).catch((err) => { reject(err) })

        })
        
        return promise;
    }
}